//
//  HNSH_ShopApplyViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/4.
//

#import "HNSH_ShopApplyViewController.h"
#import "HNSH_ShopApplyTopView.h"

#import "HNSH_ShopSubmitModel.h"

//view
#import "HNSH_ShopApplyInfoView.h"
#import "HNSH_ShopApplyImageView.h"
#import "HNSH_ShopApplyFRInfoView.h"
#import "HNSH_ShopApplyBankInfoView.h"

@interface HNSH_ShopApplyViewController ()

/* topView */
@property (nonatomic, strong) HNSH_ShopApplyTopView *topView;
/* scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;

/* firstView */
@property (nonatomic, strong) HNSH_ShopApplyInfoView *firstView;
/* secondView */
@property (nonatomic, strong) HNSH_ShopApplyImageView *secondView;
/* thirdView */
@property (nonatomic, strong) HNSH_ShopApplyFRInfoView *thirdView;
/* fourthView */
@property (nonatomic, strong) HNSH_ShopApplyBankInfoView *fourthView;

/* submitModel */
@property (nonatomic, strong) HNSH_ShopSubmitModel *submitModel;

/* nextButton */
@property (nonatomic, strong) UIButton *nextButton;
/* currenIndex */
@property (nonatomic, assign) NSInteger currenIndex;

/* rightButton */
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation HNSH_ShopApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:[self.type isEqualToString:@"20"] ? @"线下店铺申请" : @"线上店铺申请" showLeftItem:YES];
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    self.currenIndex = 0;
    
    [self configTopView];
    
    [self configScrollView];
    
    [self loadShopInfo];
}

#pragma mark configUI
- (void)configTopView {
    [self.view addSubview:self.topView];
    
    [self.view addSubview:self.nextButton];
    
    [self.view addSubview:self.rightButton];
}

- (void)configScrollView {
    [self.view addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.firstView];
    [self.scrollView addSubview:self.secondView];
    [self.scrollView addSubview:self.thirdView];
    
    if ([self.type isEqualToString:@"10"]) {
        [self.scrollView addSubview:self.fourthView];
    }
}

- (void)changeIndexPage {
    NSLog(@"changeIndexPage ==> %@",self.submitModel);
    if (self.currenIndex == 0) {//第一页
        //第一页数据
        if (![self.submitModel.name isNotBlank]) {
            [self.view makeToast:@"请选输入店铺名称"];
            return;
        }
        if (![self.submitModel.register_number isNotBlank]) {
            [self.view makeToast:@"请输入社会信用代码"];
            return;
        }
        if (![self.submitModel.license_url isNotBlank]) {
            [self.view makeToast:@"请上传营业执照"];
            return;
        }
        if (![self.submitModel.qualification_url isNotBlank]) {
            [self.view makeToast:@"请上传经营资质"];
            return;
        }
        if (![self.submitModel.authorization_url isNotBlank]) {
            [self.view makeToast:@"请上传授权书"];
            return;
        }
    }
    
    if (self.currenIndex == 1) {//第二页
        //第二页数据
        if (![self.submitModel.region_code isNotBlank] && ![self.submitModel.longitude isNotBlank]) {
            [self.view makeToast:@"请选择店铺地址"];
            return;
        }
        if (![self.submitModel.address isNotBlank]) {
            [self.view makeToast:@"请输入详细地址"];
            return;
        }
        if (![self.submitModel.category_id isNotBlank]) {
            [self.view makeToast:@"请选择店铺类型"];
            return;
        }
        if (![self.submitModel.image_url isNotBlank]) {
            [self.view makeToast:@"请上传店铺图片"];
            return;
        }
    }
    
    if (self.currenIndex == 2) {//第三页数据
        //第三页数据
        if (![self.submitModel.legal_name isNotBlank]) {
            [self.view makeToast:@"请输入法人姓名"];
            return;
        }
        if (![self.submitModel.identity_card_no isNotBlank]) {
            [self.view makeToast:@"请输入法人身份证号"];
            return;
        }
        if (![self.submitModel.identity_card_front_url isNotBlank]) {
            [self.view makeToast:@"请上传身份证头像面"];
            return;
        }
        if (![self.submitModel.identity_card_back_url isNotBlank]) {
            [self.view makeToast:@"请上传身份证国徽面"];
            return;
        }
    }
    
    if ([self.type isEqualToString:@"10"] && self.currenIndex == 3) {//第四页数据
        //第四页数据
        if (![self.submitModel.bank_name isNotBlank]) {
            [self.view makeToast:@"请输入开户银行名称"];
            return;
        }
        if (![self.submitModel.bank_branch_name isNotBlank]) {
            [self.view makeToast:@"请输入开户银行支行名称"];
            return;
        }
        if (![self.submitModel.bank_card_no isNotBlank]) {
            [self.view makeToast:@"请输入对公银行账号"];
            return;
        }
    }
    self.currenIndex ++;
    if (self.currenIndex >= self.topView.titles.count) {
        self.currenIndex --;
    }
    self.topView.currentIndex = self.currenIndex;
    [self.scrollView setContentOffset:CGPointMake(kScreenWidth*self.currenIndex, 0) animated:YES];
    if (self.currenIndex == self.topView.titles.count - 1) {
        [self.nextButton setTitle:@"提交" forState:UIControlStateNormal];
        [self loadShopCommit];//提交申请
    }
}

#pragma mark networking
//商家信息
- (void)loadShopInfo {
    [HNSH_UserNetServer requestShopInfoWithType:self.type success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            self.submitModel = [HNSH_ShopSubmitModel yy_modelWithDictionary:responseObject.resultset];
            self.firstView.submitModel = self.submitModel;
            self.secondView.submitModel = self.submitModel;
            self.thirdView.submitModel = self.submitModel;
//            self.rightButton.hidden = self.submitModel.status != 10;
            if ([self.type isEqualToString:@"10"]) {
                self.fourthView.submitModel = self.submitModel;
            }
            //状态 0:草稿 10:审核中 50:审核失败 100:已通过 110:已关闭
            if (self.submitModel.status == 10) {
                [self.rightButton setTitle:@"申请中" forState:UIControlStateNormal];
            } else if (self.submitModel.status == 50) {
                [self.rightButton setTitle:@"审核失败" forState:UIControlStateNormal];
            } else if (self.submitModel.status == 100) {
                [self.rightButton setTitle:@"审核通过" forState:UIControlStateNormal];
            } else if (self.submitModel.status == 110) {
                [self.rightButton setTitle:@"已关闭" forState:UIControlStateNormal];
            } else {
                [self.rightButton setTitle:@"" forState:UIControlStateNormal];
            }
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//提交申请
- (void)loadShopCommit {
    [HNSH_UserNetServer requestShopCommitWithParams:[self.submitModel configSubmitInfo] success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self loadShopSubmitAudit];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadShopSubmitAudit {
    [HNSH_UserNetServer requestShopSubmitAuditWithType:self.type success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [kSTWindow makeToast:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (HNSH_ShopApplyTopView *)topView {
    if (!_topView) {
        _topView = [[HNSH_ShopApplyTopView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+7, kScreenWidth, 145)];
        if ([self.type isEqualToString:@"10"]) {
            _topView.titles = @[@"店铺信息",@"店铺图片",@"法人信息",@"银行卡信息"];
        } else {
            _topView.titles = @[@"店铺信息",@"店铺图片",@"法人信息"];
        }
        _topView.currentIndex = 0;
    }
    return _topView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+145, kScreenWidth, kScreenHeight-(kNavBarAndStatusBarHeight+145)-(kBottomSafeHeight+62))];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.contentSize = CGSizeMake(kScreenWidth*self.topView.titles.count, 0);
        _scrollView.scrollEnabled = NO;
    }
    return _scrollView;
}

- (HNSH_ShopApplyInfoView *)firstView {
    if (!_firstView) {
        _firstView = [[HNSH_ShopApplyInfoView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.scrollView.height)];
        _firstView.submitModel = self.submitModel;
    }
    return _firstView;
}

- (HNSH_ShopApplyImageView *)secondView {
    if (!_secondView) {
        _secondView = [[HNSH_ShopApplyImageView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, self.scrollView.height)];
        _secondView.submitModel = self.submitModel;
    }
    return _secondView;
}

- (HNSH_ShopApplyFRInfoView *)thirdView {
    if (!_thirdView) {
        _thirdView = [[HNSH_ShopApplyFRInfoView alloc]initWithFrame:CGRectMake(kScreenWidth*2, 0, kScreenWidth, self.scrollView.height)];
        _thirdView.submitModel = self.submitModel;
    }
    return _thirdView;
}

- (HNSH_ShopApplyBankInfoView *)fourthView {
    if (!_fourthView) {
        _fourthView = [[HNSH_ShopApplyBankInfoView alloc]initWithFrame:CGRectMake(kScreenWidth*3, 0, kScreenWidth, self.scrollView.height)];
        _fourthView.submitModel = self.submitModel;
    }
    return _fourthView;
}

- (HNSH_ShopSubmitModel *)submitModel {
    if (!_submitModel) {
        _submitModel = [[HNSH_ShopSubmitModel alloc]init];
        _submitModel.type = self.type;
    }
    return _submitModel;
}

- (UIButton *)nextButton{
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextButton.frame = CGRectMake(30, kScreenHeight - 50 - 6 - kBottomSafeHeight, kScreenWidth - 60, 50);
        [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _nextButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _nextButton.backgroundColor = [UIColor appGreenColor];
        _nextButton.layer.cornerRadius = 25;
        _nextButton.layer.masksToBounds = YES;
        [_nextButton addTarget:self action:@selector(changeIndexPage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextButton;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(kScreenWidth-94, kStatusBarHeight, 94, 44);
        [_rightButton setTitle:@"申请中" forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:18];
//        _rightButton.hidden = YES;
    }
    return _rightButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
